﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SD.LLBLGen.Pro.ORMSupportClasses;
using BaseBL;
using xVal.ServerSide;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using Northwinddemo.DAL.EntityClasses;
using Northwinddemo.DAL.HelperClasses;

namespace Northwinddemo.BusinessLayer.DAL.EntityValidators
{
    [DependencyInjectionInfo(typeof(ProductsEntity), "Validator", ContextType = DependencyInjectionContextType.Singleton)]
    public class ProductsEntityValidator : ValidatorBase
    {
        // Place validation logic for candidate here
        public override void ValidateEntity(IEntityCore involvedEntity)
        {
            ProductsEntity productToValidate = (ProductsEntity)involvedEntity;
            var errors = DataAnnotationsValidationRunner.GetErrors(productToValidate);

            // Test ProductName for number occurrence.
            foreach (var ch in productToValidate.ProductName)
            {
                if (char.IsNumber(ch))
                {
                    errors.Add(new ErrorInfo(ProductsFields.ProductName.Name, "ProductName cannot contain numbers"));
                    break;
                }
            }
            if (errors.Any())
            {
                throw new RulesException(errors);
            }
            // todo: add other validation and business rules for candidate.
            base.ValidateEntity(involvedEntity);
        }
    }
}
